package com.ruoyi.api.client.service.impl;

import cn.hutool.json.JSONObject;
import com.ruoyi.api.client.domain.vo.HomeInfoVo;
import com.ruoyi.api.client.domain.vo.MineInfoVo;
import com.ruoyi.api.client.domain.vo.SysDictDataVo;
import com.ruoyi.api.client.domain.vo.UploadVo;
import com.ruoyi.api.client.service.IClientCommonService;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.oss.entity.UploadResult;
import com.ruoyi.framework.oss.service.IFileService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ISysGroupDataService;
import com.ruoyi.system.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 客户端通用模块API 服务层
 * @Author zouhuu
 * @Date 2022/8/3 10:32
 */
@Service
public class ClietnCommonServiceImpl implements IClientCommonService {

    @Autowired
    private ISysGroupDataService sysGroupDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysOssService sysOssService;

    @Autowired
    private IFileService fileService;

    /**
     * 首页信息
     * @return
     */
    @Override
    public R homeInfo() {
        // 获取首页轮播图
        List<JSONObject> banners = sysGroupDataService.selectSysGroupDataListByKey("index_banner");
        // 获取首页菜单栏
        List<JSONObject> menus = sysGroupDataService.selectSysGroupDataListByKey("index_menu");
        HomeInfoVo homeInfoVo = new HomeInfoVo(menus, banners);
        return R.ok(homeInfoVo);
    }


    /**
     * 个人中心菜单
     * @return
     */
    @Override
    public R userMenu() {
        HashMap<String, Object> map = new HashMap<>();
        // 获取首页菜单栏
        List<JSONObject> menus = sysGroupDataService.selectSysGroupDataListByKey("user_menu");
        MineInfoVo mineInfoVo = new MineInfoVo(menus);
        return R.ok(mineInfoVo);
    }

    /**
     * TODO
     * 获取首页通知
     * @return
     */
    @Override
    public R homeNotice() {
        return R.ok();
    }

    /**
     * 字典数据
     * @param dictType
     * @return
     */
    @Override
    public R dictData(String dictType) {
        List<SysDictData> sysDictDatas = dictTypeService.selectDictDataByType(dictType);
        List<SysDictDataVo> collect = sysDictDatas.stream().map(item -> {
            SysDictDataVo sysDictDataVo = new SysDictDataVo();
            BeanUtils.copyBeanProp(sysDictDataVo, item);
            return sysDictDataVo;
        }).collect(Collectors.toList());
        return R.ok(collect);
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @Override
    public R uploadFile(MultipartFile file) {
        UploadResult upload = fileService.upload(file, CommonConstants.SOURCE_CLIENT);
        UploadVo uploadVo = new UploadVo();
        BeanUtils.copyBeanProp(uploadVo, upload);
        return R.ok(uploadVo);
    }


}
